package bb.lanxing.activity.sport;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;

import com.umeng.analytics.MobclickAgent;

import bb.lanxing.R;
import bb.lanxing.common.config.UmengEventConst;
import bb.lanxing.fragment.base.BasePreferenceFragment;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.manager.SharedManager;

public class SettingTTsFragment extends BasePreferenceFragment implements Preference.OnPreferenceChangeListener {
    private EditTextPreference heartTextPreference;
    private EditTextPreference speedTextPreference;

    private RemoteSharedPreference mRemoteSharedPreference = RemoteSharedPreference.getInstance();

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getPreferenceManager().setSharedPreferencesName(RemoteSharedPreference.PREFERENCE_FILE_NAME);
        addPreferencesFromResource(R.xml.fragment_setting_tts);

        SharedManager sharedManager = SharedManager.getInstance();
        if (sharedManager.contains("key_heart_rate_warning_enable_v2")) {
            mRemoteSharedPreference.setValue("key_heart_rate_warning_enable_v2", sharedManager.getBoolean("key_heart_rate_warning_enable_v2", false));
            sharedManager.remove("key_heart_rate_warning_enable_v2");
        }
        if (sharedManager.contains(SPConstant.KEY_TTS_HEART_WARNING)) {
            mRemoteSharedPreference.setValue("key_heart_rate_warning_value", sharedManager.getInt(SPConstant.KEY_TTS_HEART_WARNING, 0) + "");
            sharedManager.remove(SPConstant.KEY_TTS_HEART_WARNING);
        }

        findPreference("key_tts_sound_enabled_v2").setOnPreferenceChangeListener(this);

        ListPreference distancePreference = (ListPreference) findPreference("key_tts_distance");
        updateListSummary(distancePreference, distancePreference.getValue());
        distancePreference.setOnPreferenceChangeListener(this);

        ListPreference durationPreference = (ListPreference) findPreference("key_tts_duration");
        updateListSummary(durationPreference, durationPreference.getValue());
        durationPreference.setOnPreferenceChangeListener(this);

        findPreference("key_gps_sound_enabled_v2").setOnPreferenceChangeListener(this);

        findPreference("key_heart_rate_warning_enable_v2").setOnPreferenceChangeListener(this);

        heartTextPreference = (EditTextPreference) findPreference("key_heart_rate_warning_value");
        heartTextPreference.setSummary(getString(R.string.sport_setting_tts_heart_limit_value_unit, mRemoteSharedPreference.getHeartRateWarningValue() + ""));
        heartTextPreference.setOnPreferenceChangeListener(this);

        findPreference("key_speed_warning_enable_v2").setOnPreferenceChangeListener(this);

        speedTextPreference = (EditTextPreference) findPreference("key_speed_warning_value");
        speedTextPreference.setSummary(getString(R.string.sport_setting_tts_speed_limit_value_unit, mRemoteSharedPreference.getSpeedWarningValue() + ""));
        speedTextPreference.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object obj) {
        mRemoteSharedPreference.setValue(preference.getKey(), obj);
        boolean booleanValue = Boolean.parseBoolean(String.valueOf(obj));
        switch (preference.getKey()) {
            case "key_tts_sound_enabled_v2":
                if (booleanValue) {
                    MobclickAgent.onEventValue(getActivity(), UmengEventConst.V30_SETTING_VOICE_OPEN, null, 1);
                }
                break;
            case "key_tts_distance":
                updateListSummary((ListPreference) preference, obj.toString());
                break;
            case "key_tts_duration":
                updateListSummary((ListPreference) preference, obj.toString());
                break;
            case "key_gps_sound_enabled_v2":
                if (booleanValue) {
                    MobclickAgent.onEventValue(getActivity(), UmengEventConst.V30_SETTING_GPS_LOCATION_GPS, null, 1);
                }
                break;
            case "key_heart_rate_warning_enable_v2":
                if (booleanValue) {
                    MobclickAgent.onEventValue(getActivity(), UmengEventConst.V30_SETTING_SPEED_OPEN, null, 1);
                }
                break;
            case "key_heart_rate_warning_value":
                if (booleanValue) {
                    MobclickAgent.onEventValue(getActivity(), UmengEventConst.V30_SETTING_BPM_OPEN, null, 1);
                }
                heartTextPreference.setSummary(getString(R.string.sport_setting_tts_heart_limit_value_unit, obj.toString()));
                break;
            case "key_speed_warning_value":
                speedTextPreference.setSummary(getString(R.string.sport_setting_tts_speed_limit_value_unit, obj.toString()));
                break;
        }
        return true;
    }

    private void updateListSummary(ListPreference listPreference, String str) {
        int findIndexOfValue = listPreference.findIndexOfValue(str);
        CharSequence[] entries = listPreference.getEntries();
        if (findIndexOfValue < 0) {
            findIndexOfValue = 0;
        }
        listPreference.setSummary(entries[findIndexOfValue]);
    }
}